const app = getApp();

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  type: null,
};

var currentPage = {
  data,
  listParams:{},
  reqUrl: app.serAddrs.getInviteCoupontList,
  onLoad,
  listDataInit,
  pageListInit,
  getPageList,
  scrolltolower,
  onShareAppMessage: function () {
    const that = this;
    const userId = app.userInfo && app.userInfo.userId || "";
    const activityUuid = that.data.activityUuid;
    const path = `${app.xhx_route.leagueList}?invitorUuid=${userId}&activityUuid=${activityUuid}`;
    console.log(path)
    return {
      title: '',
      path
    }
  }
};

var page = Object.assign({}, currentPage);

Page(page)

function onLoad(options) {
  console.log('=====================', options)
  const that = this;
  const type = options && options.type || 1;
  const activityUuid = options && options.activityUuid || '';
  const amount = options && options.amount || 0;
  if(type == 2){
    wx.setNavigationBarTitle({
      title: '成功邀请的用户',
    })
  }
  this.setData({
    type,
    activityUuid,
    amount
  })
  that.listParams.status = type-1;
  that.pageListInit();
}

function listDataInit() {
  const that = this;
  that.setData({
    lists: [], //列表数据
    msg: '请稍等...', //提示信息
    footMsg: "到底啦！", //底部提示信息
  })

  that.listParams.keyword = ''; //搜索框输入值
  that.listParams.page = 1; //当前页数
  that.listParams.row = 10; //1页的条数
  that.listParams.isReach = false; //是否全部加载完毕
  return true;
}

function pageListInit() {
  const that = this;
  that.listDataInit();
  that.getPageList();
  return true;
}

/**
 * 获取列表
 * param isReachBottom [Boolean] 是否上拉加载触发
 */
function getPageList(isReachBottom) {
  var that = this;
  var lists = that.data.lists;
  var listParams = that.listParams;
  var page = listParams.page;
  var row = listParams.row;
  var isReach = listParams.isReach;
  var footMsg = "";

  if (isReachBottom) {
    if (isReach) return false;
    page++;
  }

  if (page == 1) {
    lists = [];
  }

  listParams.pageSize = row;
  listParams.pageNum = page;

  app.middleware.requestAjax({
    serAddr: that.reqUrl,
    data: listParams,
    cancelToast: true,
    success: function (res) {
      console.log(res)
      var tempLists = res.data || [];

      if (row <= tempLists.length) {
        isReach = false;
        footMsg = "上拉加载更多";
      } else {
        isReach = true;
        footMsg = "没有更多数据啦！";
      }
      that.setData({
        msg: "",
        lists: lists.concat(tempLists),
        footMsg: footMsg,
      })
      that.listParams.page = page;
      that.listParams.isReach = isReach;

      if (that.callBack) that.callBack(res, lists);
      if (that.firstCallBack && page == 1) that.firstCallBack(res);
    },
    fail: function (res) {

      if (isReachBottom) {
        that.setData({
          footMsg: res && res.msg || "加载失败",
        })
      } else {
        that.setData({
          msg: res && res.msg || "加载失败",
        })
      }

    }
  })
}

function scrolltolower() {
  const that = this;
  that.getPageList(true);
}